package customer_service

import (
	"beta/models"
	"beta/pkg/e"
	"beta/pkg/logger"
	"beta/pkg/util"
	"github.com/tidwall/gjson"
	"time"
)

type CustomerService struct {
	FullName string
	Hosts map[string]*models.AppEnv
	TraceId string
}

type CC  struct {
	FullName             string `json:"fullName"`
	LocationCode         string `json:"locationCode"`
	BizLocationCode      string `json:"bizLocationCode"`
	Nsrsbh               string `json:"nsrsbh"`
	Shxydm               string `json:"shxydm"`
	InternalUsableStatus string `json:"internalUsableStatus"`
	Source               string `json:"source"`
}

func (c CustomerService) AddCustomer() (customerId string, err error){
	appName := "cc"
	cc := CC{
		FullName:             c.FullName,
		LocationCode:         "130982",
		BizLocationCode:      "130000",
		Nsrsbh:               "",
		Shxydm:               "",
		InternalUsableStatus: "Y",
		Source:               "214649602167",
	}

	url := c.Hosts[appName].AppDomain + "/cc-production/test/company/saveOrUpdateAgency"

	headers := e.GetHeaderMap(e.JsonHeader)
	headers["appId"] = "20010100010000"
	headers["timestamp"] = "1467791956367"
	headers["appKey"] = "48778FFD44ADE08A462BF1EF3085BBD2D02613EF"

	logger.ZapLogger.Infow("【创建客户】", "请求参数", cc, "traceId", c.TraceId)
	httpClient := util.NewHttpClient(3 * time.Second).R()

	customerResp, respErr := httpClient.SetHeaders(headers).SetBody(cc).Post(url)
	logger.ZapLogger.Infow("【创建客户】", "请求结果", customerResp, "traceId", c.TraceId)
	if respErr != nil {
		return "", respErr
	}

	customerId = gjson.Get(customerResp.String(), "data").String()
	return customerId, nil
}